package com.mewp.permissions.system.controller;

import com.mewp.permissions.common.entity.Result;
import com.mewp.permissions.model.po.SysMenu;
import com.mewp.permissions.model.vo.AssginMenuVo;
import com.mewp.permissions.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author mewp
 * @version 1.0
 * @date 2023/7/28 16:26
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/admin/system/menu")
public class SysMenuController {
    @Resource
    private SysMenuService menuService;

    @ApiOperation(value = "获取菜单")
    @GetMapping("findNodes")
    public Result<List<SysMenu>> findNodes() {
        List<SysMenu> list = menuService.findNodes();
        return Result.ok(list);
    }

    @ApiOperation(value = "新增菜单")
    @PostMapping("/")
    @ApiImplicitParam(name = "permission", value = "菜单信息", paramType = "body", required = true)
    public Result<Boolean> save(@RequestBody SysMenu permission) {
        boolean result = menuService.save(permission);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "修改菜单")
    @PutMapping("/")
    @ApiImplicitParam(name = "permission", value = "菜单信息", paramType = "body", required = true)
    public Result<Boolean> updateById(@RequestBody SysMenu permission) {
        boolean result = menuService.updateById(permission);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "菜单Id", paramType = "path", required = true)
    public Result<Boolean> remove(@PathVariable Long id) {
        boolean result = menuService.removeById(id);
        return result ? Result.ok() : Result.fail();
    }

    @ApiOperation(value = "根据角色获取菜单")
    @GetMapping("/assign/{roleId}")
    @ApiImplicitParam(name = "roleId", value = "角色Id", paramType = "path", required = true)
    public Result<List<SysMenu>> toAssign(@PathVariable Long roleId) {
        List<SysMenu> list = menuService.findMenuByRoleId(roleId);
        return Result.ok(list);
    }

    @ApiOperation(value = "给角色分配权限")
    @PostMapping("/assign")
    public Result<Boolean> doAssign(@Valid @RequestBody AssginMenuVo assetsMenuVo) {
        boolean result = menuService.doAssign(assetsMenuVo);
        return result ? Result.ok() : Result.fail();
    }
}
